using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using IRQ.GPS.OpenStreetMap;
using Microsoft.WindowsMobile.Samples.Location;
using SimpleGPSApp.Base;
using IRQ.BaseFunctions.Drawing;

namespace SimpleGPSApp.Controls
{
    public partial class CtrlMap : UserControl
    {
        #region Fields
        private TileManager tileManager = new TileManager("http://tile.openstreetmap.org/", ApplicationHelper.StartupPath + "\\Mapa");
        GpsPosition position = null;
        PointF gpsPosition = new PointF();
        int zoom = 0;
        #endregion

        #region Properties
        public GpsPosition Position
        {
            set 
            {
                position = value;
                if (position != null)
                {
                    LoadMap();
                }
            }
        }
        #endregion

        public CtrlMap()
        {
            InitializeComponent();
        }

        public void LoadMap()
        {
            if (tileManager == null)
            { 
                tileManager = new TileManager("http://tile.openstreetmap.org/", ApplicationHelper.StartupPath + "\\Mapa");
            }

            if (Directory.Exists(tileManager.CacheDirectory))
            {
                if (position != null)
                {
                    gpsPosition.X = (float)position.Longitude;
                    gpsPosition.Y = (float)position.Latitude;
                }
                else
                {
                    gpsPosition.X = 20.90049F;
                    gpsPosition.Y = 52.22250F;
                }

                PointF point = TileCoordinatesConverter.GetTileIndex(zoom, gpsPosition);
                Tile tile = tileManager.GetTile(zoom, (int)point.X, (int)point.Y, true);
                if (tile != null && tile.Picture != null)
                {
                    DrawMarker(tile.Picture, point);

                    this.pictureBox_map.Size = new Size(tile.Picture.Width, tile.Picture.Height);
                    this.pictureBox_map.SizeMode = PictureBoxSizeMode.StretchImage;
                    this.pictureBox_map.Image = new Bitmap(tile.Picture);
                }
            }
        }

        private void DrawMarker(Bitmap _bitmap, PointF _tileCoord)
        {
            Graphics g = Graphics.FromImage(_bitmap);
            Point marker = new Point();
            marker.X = (int)( (_tileCoord.X - (int)_tileCoord.X)*_bitmap.Width );
            marker.Y = (int)((_tileCoord.Y - (int)_tileCoord.Y) * _bitmap.Height);

            g.FillEllipse(new SolidBrush(Color.Navy), marker.X, marker.Y, 10, 10);
            g.Dispose();
        }

        private void button_zoomIn_Click(object sender, EventArgs e)
        {
            if (Root.ApplicationSettings != null)
            {
                zoom++;
                LoadMap();
            }
        }

        private void button_zoomOut_Click(object sender, EventArgs e)
        {
            if (Root.ApplicationSettings != null)
            {
                zoom--;
                zoom = (zoom < 1) ? 1 : zoom;
                
                LoadMap();
            }
        }
    }
}
